package com.aistar.controller;

import com.aistar.pojo.Choice;
import com.aistar.pojo.ChoiceCollect;
import com.aistar.pojo.Customer;
import com.aistar.service.ChoiceCollectService;
import com.aistar.util.MessageUtil;
import com.aistar.util.RedisMessage;
import com.aistar.util.RedisUtil;
import com.aistar.util.ServerResponse;
import org.apache.catalina.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/choose/chooseCollect")
public class ChoiceCollectController {

    @Autowired
    public ChoiceCollectService choiceCollectService;

   /* @RequestMapping("/add")
    @ResponseBody
    public ServerResponse add(ChoiceCollect choiceCollect, HttpServletRequest request){
        //获得当前登录用户的id
        Customer customer=(Customer)request.getSession().getAttribute("loginCustomer");
        Integer customerId=customer.getCustomerId();
        System.out.println("登录用户id:" + customerId);

        choiceCollect.setCustomerId(customerId);
        choiceCollect.setChoiceCollectTime(new Date());

        Choice choice=new Choice();
        choiceCollect.setChoiceId(choice.getChoiceId());
        System.out.println(choiceCollect);
        return choiceCollectService.add(choiceCollect);
    }
*/
    @RequestMapping
    @ResponseBody
    public ServerResponse getAll(){
        ServerResponse serverResponse=choiceCollectService.getAll();
        List<ChoiceCollect> choiceCollectList=(List<ChoiceCollect>) serverResponse.getData();
        if (choiceCollectList!=null && choiceCollectList.size()>0)
            return ServerResponse.getSuccess(choiceCollectList);
        return ServerResponse.getFailed();
    }

    @RequestMapping("{id}")
    @ResponseBody
    public ServerResponse getById(@PathVariable("id")Integer choiceCollectId){
        return choiceCollectService.getById(choiceCollectId);
    }


    @RequestMapping("/{customerId}/{choiceId}")
    @ResponseBody
    public ServerResponse choiceCollect(@PathVariable("customerId")Integer customerId,@PathVariable("choiceId")Integer choiceId){
        String field=customerId+ RedisMessage.SEPARATOR+choiceId;
        String value=(String)RedisUtil.hget(RedisMessage.CHOICE_COLLECT_KEY,field);

        int valueType;
        //如果redis缓存中没有数据
        if(value == null){
            ServerResponse serverResponse = choiceCollectService.getByCusAndCol(customerId, choiceId);
            if(MessageUtil.STATUS_SUCCESS.equals(serverResponse.getStatus()))//判断数据库里面有没有数据，有的话
                valueType=((ChoiceCollect)serverResponse.getData()).getStatus() == 0 ? 1:0;
            else
                valueType=1;//如果数据库中没有数据，则变为第一条数据
        }else//如果redis缓存中有数据
            valueType=value.split(RedisMessage.SEPARATOR)[0].equals("0") ? 1 : 0;//改变redis缓存里面数据的状态
        value=valueType+RedisMessage.SEPARATOR+new Date().getTime();//再将修改后的数据重新赋值给value

        if(RedisUtil.hset(RedisMessage.CHOICE_COLLECT_KEY,field,value))
            return ServerResponse.modifySuccess(valueType ==1 ? 1:-1); //点赞状态为已点赞时，点赞数加一，再点一下，则点赞数减一
        return ServerResponse.modifyFailed();

    }









}
